<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<script src="../../../resources/js-test.js"></script>
</head>
<body onload="allDone()">
Tests that the document's readyState is set properly at various phases during load.
<div id="console"></div>
<script defer src="data:text/javascript,shouldBeEqualToString('document.readyState', 'interactive');shouldBeTrue('readyStateChangeFired');"></script>
<script src="data:text/javascript,shouldBeEqualToString('document.readyState', 'loading');"></script>
<iframe src="does-not-exist.html"></iframe>
<script>
  var jsTestIsAsync = true;

  shouldBe("document.onreadystatechange", "null");
  shouldBeEqualToString("document.readyState", "loading");

  document.addEventListener("DOMContentLoaded", function() {
    shouldBeEqualToString("document.readyState", "interactive");

    var el = document.createElement('script');
    el.src = "data:text/javascript,shouldBeEqualToString('document.readyState', 'interactive');";
    document.getElementsByTagName('head')[0].appendChild(el);
  }, false);

  var expectedStates = ["interactive", "complete"];
  var nextExpectedState = 0;
  var readyStateChangeFired = false;
  document.onreadystatechange = function(event) {
    shouldBe("event.target.readyState", "expectedStates[nextExpectedState]");
    ++nextExpectedState;
    readyStateChangeFired = true;
  }

  function readyStateShouldNotChange(event) {
    testFailed("should not have been notified of subdocument state change to " + event.target.readyState);
  }

  var subdocument = document.getElementsByTagName('iframe')[0].contentDocument;
  subdocument.onreadystatechange = readyStateShouldNotChange;
  subdocument.open();
  shouldBeEqualToString("subdocument.readyState", "loading");
  subdocument.close();
  shouldBeEqualToString("subdocument.readyState", "complete");

  var htmlDocument = document.implementation.createHTMLDocument();
  shouldBeEqualToString("htmlDocument.readyState", "loading");

  var expectedDynamicStates = ["interactive", "complete"];
  var nextExpectedDynamicState = 0;
  var dynamicIframe = document.createElement('iframe');
  dynamicIframe.setAttribute("src", "about:blank");
  dynamicIframe.setAttribute("onload", "parent.dynamicIframeLoaded()");
  subdocument.body.appendChild(dynamicIframe);
  function dynamicIframeLoaded() {
    shouldBeEqualToString("dynamicIframe.contentDocument.readyState", "complete");
    dynamicIframe.contentDocument.onreadystatechange = readyStateShouldNotChange;
    dynamicIframe.contentDocument.open();
    shouldBe("dynamicIframe.contentDocument.onreadystatechange", "null");
    shouldBeEqualToString("dynamicIframe.contentDocument.readyState", "loading");
    dynamicIframe.contentDocument.onreadystatechange = function() {
      shouldBe("dynamicIframe.contentDocument.readyState", "expectedDynamicStates[nextExpectedDynamicState]");
      ++nextExpectedDynamicState;
    }
    dynamicIframe.contentDocument.close();
    shouldBeEqualToString("dynamicIframe.contentDocument.readyState", "complete");

    dynamicIframe.contentDocument.onreadystatechange = readyStateShouldNotChange;
    subdocument.open();
    subdocument.close();
  }

  function allDone() {
    shouldBe("nextExpectedState", "expectedStates.length");
    shouldBe("nextExpectedDynamicState", "expectedDynamicStates.length");
    shouldBeEqualToString("document.readyState", "complete");
    finishJSTest();
  }
</script>
</body>
</html>
